﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopperVineSoftware.Stackr.Parser.Lexemes
{
    /// <summary>Statement invoking a definition by name.</summary>
    public class NamedValue : Statement
    {
        private readonly string name;

        /// <summary>Name of definition to invoke.</summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>Constructor.</summary>
        /// <param name="name">Name of definition to invoke.</param>
        /// <exception cref="ArgumentNullException">If the <paramref name="name"/> parameter is <c>null</c> or empty.</exception>
        public NamedValue(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            this.name = name;
        }

        /// <summary>Determines whether two named values are equal.</summary>
        /// <remarks>
        ///     Two named values are considered equal if and only if they invoke the same name.
        /// </remarks>
        /// <param name="obj">Object to test for equality.</param>
        /// <returns>True if the <paramref name="obj"/> parameter is equal to this named value.</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is NamedValue))
                return false;

            return ((obj as NamedValue).name == this.name);
        }

        /// <summary>Retrieves a hash value for this named value.</summary>
        /// <returns>A hash code for this named value.</returns>
        public override int GetHashCode()
        {
            return this.name.GetHashCode();
        }

        /// <summary>Retrieves a string representation of this named value.</summary>
        /// <returns>A string representation of this named value.</returns>
        public override string ToString()
        {
            return this.name;
        }
    }
}
